import java.awt.*;

public class MyLinkedList {

        class Node{
            Node next;
            int val;
            public Node(int val){
                this.val = val;
            }
        }
        int size;
        Node head;
        public MyLinkedList(){
            this.size = 0;
            this.head = null;
        }


        public int get(int index) {
            if(head==null) return -1;
            if(index<0||index>=size) return -1;
            Node cur = head;
            while(index!=0){
                cur = cur.next;
                index--;
            }
            return cur.val;
        }

        public void addAtHead(int val) {
            Node node = new Node(val);
            node.next = head;
            head = node;
            size++;
        }

        public void addAtTail(int val) {
            Node node = new Node(val);
            if(head==null){
                head = node;
                size++;
            }else{
                Node cur = head;
                while(cur.next!=null){
                    cur = cur.next;
                }
                cur.next = node;
                size++;
            }
        }

        public void DisplayList(){
            if(head==null) return;
            Node cur = head;
            while(cur!=null){
                System.out.print(cur.val+" ");
                cur = cur.next;
            }
            System.out.println();
        }

        public Node findIndexSubOfOne(int index){
            Node cur = head;
            while(index-1!=0){
                cur = cur.next;
            }
            return cur;
        }
        public void addAtIndex(int index, int val) {
            if(index<0||index>size){
                return;
            }
            if(index==0){
                addAtHead(val);
                size++;
                return;
            }
            if(index==size){
                addAtTail(val);
                size++;
                return;
            }
            Node prev = findIndexSubOfOne(index);
            Node node = new Node(val);
            node.next = prev.next;
            prev.next = node;
            size++;
        }

        public void deleteAtIndex(int index) {
            if(index<0||index>=size){
                return;
            }
            if(index==0){
                if(size!=1){
                    head = head.next;
                    size--;
                    return;
                }else{
                    head = null;
                    size--;
                    return;
                }
            }
            Node prev = head;
            for(int i = 0;i<index-1;i++){
                prev = prev.next;
            }
            Node deleteNode = prev.next;
            prev.next = deleteNode.next;
            size--;
            return;
        }
    }

